#ifndef __MODEL_H__
#define __MODEL_H__

#include "./animationhelper.h"

typedef void (Delegater::*OnRenderMaterialFunc)(int texid);

typedef float vector3d[3];
typedef float vector2d[2];
typedef unsigned short short3d[3];

struct ModelData
{
	string strName;
	bool ispanel;

	unsigned short nfaces;
	unsigned short nvertices;

	vector3d* vertices;
	vector3d* normals;
	vector2d* texcoords;
	short3d* indexs;

	int material;

	float matrix[4][4];

	Color color;

	ModelData()
	{
		vertices = NULL;
		normals = NULL;
		texcoords = NULL;
		indexs = NULL;
	}
};

typedef vector<ModelData> ModelList;
typedef vector<ModelData>::iterator ModelListIter;

class Model
{
	friend class AnimationHelper;
public:
	Model();
	~Model();

	void LoadBy3dsFile(Lib3dsFile* pFile);
	void LoadByModel(Model& model);
	void Render();

	bool GetPanelFace(string strBkgName, float points[3][3]);
	AnimationHelper* GetAnimationHelper();

public:
	Event<OnRenderMaterialFunc> OnRenderMaterial;

private:
	void RenderModel(ModelData& data);
	void LoadMesh(Lib3dsMesh* p3dsMesh, ModelData& data);

	//calculate normals by vertexs
	void CalcNormalsByVertexs(Lib3dsMesh* p3dsMesh, ModelData& data);
	void MatrixAdd(vector3d m, vector3d a, vector3d b);
	void CalcNormal(float v[3][3], float out[3]);
	void ReduceToUnit(float vector[3]);

	ModelData* GetModelByName(string strName);
private:
	ModelList m_datalist;
	AnimationHelper m_animationHelper;
};

#endif